package com.yixiaobai.swimming.controller;

import com.yixiaobai.swimming.api.EmployerApi;
import com.yixiaobai.swimming.common.ResponseData;
import com.yixiaobai.swimming.dto.request.AddEmployerReqDto;
import com.yixiaobai.swimming.dto.request.DeleteEmployerReqDto;
import com.yixiaobai.swimming.dto.request.RegisterReqDto;
import com.yixiaobai.swimming.error.ErrorCode;
import com.yixiaobai.swimming.service.EmployerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuyunlong
 * @date 2020/7/4 1:14 下午
 * @description
 */
@RestController
public class EmployerController implements EmployerApi {

    @Autowired
    private EmployerService employerService;

    @Override
    public ResponseData<String> register(@RequestBody RegisterReqDto registerReqDto) throws Exception{
        Boolean requiredParams = StringUtils.isBlank(registerReqDto.getUsername())
                || StringUtils.isBlank(registerReqDto.getPassword())
                || StringUtils.isBlank(registerReqDto.getRealName())
                || StringUtils.isBlank(registerReqDto.getPhone());
        if (requiredParams) {
            return ResponseData.buildErr(ErrorCode.PARAM_ERROR);
        }
        return employerService.register(registerReqDto);
    }

    @Override
    public ResponseData<String> login(@RequestParam(value = "username") String username,
                                      @RequestParam(value = "password") String password) {
        Boolean requiredParams = StringUtils.isBlank(username)
                || StringUtils.isBlank(password);
        if (requiredParams) {
            return ResponseData.buildErr(ErrorCode.PARAM_ERROR);
        }
        return employerService.login(username, password);
    }

    @Override
    public ResponseData<String> addEmployerV1(@RequestBody AddEmployerReqDto addEmployerReqDto) {
        Boolean requiredParams = StringUtils.isBlank(addEmployerReqDto.getAddUser())
                || StringUtils.isBlank(addEmployerReqDto.getUsername())
                || StringUtils.isBlank(addEmployerReqDto.getRealName())
                || StringUtils.isBlank(addEmployerReqDto.getPhone());
        if (requiredParams) {
            return ResponseData.buildErr(ErrorCode.PARAM_ERROR);
        }
        String phone = addEmployerReqDto.getPhone();
        if (!StringUtils.isNumeric(phone) && phone.length() != 11) {
            return ResponseData.buildErr(ErrorCode.PHONE_ERROR);
        }
        return employerService.addEmployerV1(addEmployerReqDto);
    }

    @Override
    public ResponseData<String> deleteEmployerV1(DeleteEmployerReqDto deleteEmployerReqDto) {
        Boolean requiredParams = StringUtils.isBlank(deleteEmployerReqDto.getDeleteUser())
                || StringUtils.isBlank(deleteEmployerReqDto.getUsername());
        if (requiredParams) {
            return ResponseData.buildErr(ErrorCode.PARAM_ERROR);
        }
        return employerService.deleteEmployerV1(deleteEmployerReqDto);
    }
}
